
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;



public class Worker {

	OrderList allOrders;

	ItemList allItems;
	String reportProcess="\n\t PROCESSED ORDERS \n =====================================================================\n" +
			"Order ID  Customer ID   Item ID   Quantity   Initial Price   Final Price \n";
	String reportFailed="\n\n\t FAILED ORDERS \n =====================================================================\n";
	double finalPrice, totalGain, totalDiscount;
	int totalDeliveredOrders, totalFailedOrders;



	public Worker(OrderList allOrders, ItemList allItems) {

		this.allOrders = allOrders;
		this.allItems= allItems;

	}

	public void run() {

		System.out.println("Processing");
		while (allOrders.hasOrder()) {
			processOneOrder();
		}
		printStatistics();
		writeReport("Report.txt", reportProcess + reportFailed);
		//just to see the hashMap values after the orders are processed
		//processOneItem();



	}

	public void processOneOrder() {

		Order order = allOrders.getNextOrder() ;
		//process the order here
		String itemId=order.getItemID();
		Item m=allItems.getItembyId(itemId);
		int oQuantity=order.getOrderQuantity();
		try{
		int wQuantity= m.getQuantity();
		if (wQuantity >= oQuantity){
			wQuantity = wQuantity-oQuantity;
			m.setQuantity(wQuantity);
			reportProcess+=String.format("%-10s",order.getOrderID())+String.format("%-14s",order.getCustomerID()) +String.format("%-10s",order.getItemID())+
					String.format("%-11s",+order.getOrderQuantity());
			double basicPrice = order.getOrderQuantity() * m.getPrice();
			double finalPrice = calculateDiscountPrice (basicPrice, order.getCustomerID(), order.getOrderQuantity());
			reportProcess+= String.format("%-16s",String.format("%.2f",basicPrice)+"   GBP")+ String.format("%-16s",String.format("%.2f",finalPrice)+ "   GBP ")+"\n"; 
			totalGain += finalPrice;
			totalDiscount +=  basicPrice - finalPrice;
			totalDeliveredOrders++;
		}
		else 
		{reportFailed += "Order " + order.getOrderID()+ " for "+order.getOrderQuantity()+" items (Item ID "+
				order.getItemID()+ ") could not be processed due to item shortage in warehouse\n";
		totalFailedOrders++;}
		}
		catch (NullPointerException npe){
			System.out.println("item id "+order.getItemID() + " cannot be found in warehouse");
		}
	}





	public void writeReport(String filename, String report) {

		FileWriter writer;
		try {

			writer = new FileWriter(filename);
			writer.write(report);
			writer.close();
		}
		//if the file has not been found catch the exception and display the suitable message
		catch (FileNotFoundException fnf){
			System.out.println(filename + " not found ");
			System.exit(0);
		}
		// this catch clause, manages the possibility of Stack overflow during IO process
		catch (IOException ioe){
			ioe.printStackTrace();
			System.exit(1);
		}
	}

	public void processOneItem() {

		allItems.hashPrint();

	}


	private double calculateDiscountPrice(double bPrice, String cId, int quantity){
		double price = 0;
		if (cId.charAt(0)=='A'){
			price =bPrice - bPrice * 15/100;
		}
		else if (cId.charAt(0)=='B'){
			price =bPrice - bPrice * 5/100;
		}
		else price=bPrice;
		if (quantity>=100){
			price =price - price * 10/100;
		}
		else if (quantity<100 && quantity>=60){
			price =price - price * 5/100;
		}
		return price;
	}

	private void printStatistics(){
		try{
		double successPercentage = 100 * totalDeliveredOrders / (totalDeliveredOrders + totalFailedOrders);
		reportProcess += "\n Total Gain for our warehouse : " + String.format("%.2f",totalGain)+" GBP";
		reportProcess += "\n Total Discount amount for delivered orders : " + String.format("%.2f",totalDiscount)+" GBP";
		reportProcess += "\n Delivered Orders : " + totalDeliveredOrders ;
		reportProcess += "\n Unsuccessful Orders : " + totalFailedOrders + "\n";
		reportProcess += "\n Successful Orders percentage: " + successPercentage  + "%\n";
		}
		catch(ArithmeticException ae){
			System.out.println("No orders to process");
		}




	}





}